<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/8
 * Time: 16:55
 */

namespace app\api\controller;


use think\Db;

class Cases extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 案例列表
     * @return \think\response\Json
     * type     1 点即丽   2 花样青青
     * limit    1 点即丽首页展示3条    2 列表
     * page     页码
     */
    public function caseList(){
        $type = input('type') ? input('type') : 1; // 1 点即丽   2 花样青青
        $limit = input('limit'); // 1 点即丽首页展示3条   2 列表
        
        $query = Db::table('up_case')->where('case_type', $type)->field('case_id, case_thumb, case_title');
        if($limit == 1){
            $query->limit(3);
        }else{
            $query->page($this->page, 10);
        }
        $data = $query->order('case_time desc')->select();
        if($data){
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 案例详情
     * @return \think\response\Json
     * case_id      案例ID
     */
    public function caseDetail(){
        header("Access-Control-Allow-Origin: *");
        $case_id = input('case_id');
        // 增加浏览量
        Db::table('up_case')->where('case_id', $case_id)->setInc('case_view');
        $detail = Db::table('up_case')
            ->where('case_id', $case_id)
            ->field('case_images, case_title, case_content, case_view, case_date')
            ->find();
        if(!empty($detail['case_images'])){
            $detail['case_images'] = explode(',', $detail['case_images']);
        }else{
            $detail['case_images'] = [];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
}